package cn.mes.module.mes.controller.admin.baseworkplan;

import cn.mes.module.mes.utils.kingdee.KingDeeGetDataUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.CommonResult;
import cn.mes.framework.common.util.object.BeanUtils;
import static cn.mes.framework.common.pojo.CommonResult.success;

import cn.mes.framework.excel.core.util.ExcelUtils;

import cn.mes.framework.apilog.core.annotation.ApiAccessLog;
import static cn.mes.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.mes.module.mes.controller.admin.baseworkplan.vo.*;
import cn.mes.module.mes.dal.dataobject.baseworkplan.BaseWorkPlanDO;
import cn.mes.module.mes.dal.dataobject.baseworkplan.BaseBomChildDO;
import cn.mes.module.mes.service.baseworkplan.BaseWorkPlanService;

@Slf4j
@Tag(name = "管理后台 - 任务单信息")
@RestController
@RequestMapping("/mes/base-work-plan")
@Validated
public class BaseWorkPlanController {

    @Resource
    private BaseWorkPlanService baseWorkPlanService;
    @Resource
    private KingDeeGetDataUtils kingDeeGetDataUtils;

    @PostMapping("/create")
    @Operation(summary = "创建任务单信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:create')")

    public CommonResult<String> createBaseWorkPlan(@Valid @RequestBody BaseWorkPlanSaveReqVO createReqVO) {
        return success(baseWorkPlanService.createBaseWorkPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新任务单信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:update')")
    public CommonResult<Boolean> updateBaseWorkPlan(@Valid @RequestBody BaseWorkPlanSaveReqVO updateReqVO) {
        baseWorkPlanService.updateBaseWorkPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除任务单信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:delete')")
    public CommonResult<Boolean> deleteBaseWorkPlan(@RequestParam("id") String id) {
        baseWorkPlanService.deleteBaseWorkPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得任务单信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:query')")
    public CommonResult<BaseWorkPlanRespVO> getBaseWorkPlan(@RequestParam("id") String id) {
        BaseWorkPlanDO baseWorkPlan = baseWorkPlanService.getBaseWorkPlan(id);
        return success(BeanUtils.toBean(baseWorkPlan, BaseWorkPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得任务单信息分页")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:query')")
    @ApiAccessLog(operateType =GET )
    public CommonResult<PageResult<BaseWorkPlanRespVO>> getBaseWorkPlanPage(@Valid BaseWorkPlanPageReqVO pageReqVO) {
//        List<TIcitem> tIcitemDaoList=this.tIcitemService.getWorkPlanList();
//        log.info("Tictem:{}", JSONObject.toJSONString(tIcitemDaoList));
        PageResult<BaseWorkPlanDO> pageResult = baseWorkPlanService.getBaseWorkPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseWorkPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出任务单信息 Excel")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseWorkPlanExcel(@Valid BaseWorkPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseWorkPlanDO> list = baseWorkPlanService.getBaseWorkPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "任务单信息.xls", "数据", BaseWorkPlanRespVO.class,
                        BeanUtils.toBean(list, BaseWorkPlanRespVO.class));
    }

    @GetMapping("/synchronization")
    @Operation(summary = "工单信息同步")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:synchronization')")
    public void synchronizationBaseWorkPlan(@Valid BaseWorkPlanSynchronizationVO baseWorkPlanSynchronizationVO) throws IOException {
            kingDeeGetDataUtils.getWorkPlanInfo(baseWorkPlanSynchronizationVO);

    }


    // ==================== 子表（投料单详细信息） ====================

    @GetMapping("/base-bom-child/page")
    @Operation(summary = "获得投料单详细信息分页")
    @Parameter(name = "workPlanInterId", description = "投料单内码")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:query')")
    public CommonResult<PageResult<BaseBomChildDO>> getBaseBomChildPage(PageParam pageReqVO,
                                                                                        @RequestParam("workPlanInterId") String workPlanInterId) {
        return success(baseWorkPlanService.getBaseBomChildPage(pageReqVO, workPlanInterId));
    }

    @PostMapping("/base-bom-child/create")
    @Operation(summary = "创建投料单详细信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:create')")
    public CommonResult<String> createBaseBomChild(@Valid @RequestBody BaseBomChildDO baseBomChild) {
        return success(baseWorkPlanService.createBaseBomChild(baseBomChild));
    }

    @PutMapping("/base-bom-child/update")
    @Operation(summary = "更新投料单详细信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:update')")
    public CommonResult<Boolean> updateBaseBomChild(@Valid @RequestBody BaseBomChildDO baseBomChild) {
        baseWorkPlanService.updateBaseBomChild(baseBomChild);
        return success(true);
    }

    @DeleteMapping("/base-bom-child/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除投料单详细信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:delete')")
    public CommonResult<Boolean> deleteBaseBomChild(@RequestParam("id") String id) {
        baseWorkPlanService.deleteBaseBomChild(id);
        return success(true);
    }

	@GetMapping("/base-bom-child/get")
	@Operation(summary = "获得投料单详细信息")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-work-plan:query')")
	public CommonResult<BaseBomChildDO> getBaseBomChild(@RequestParam("id") String id) {
	    return success(baseWorkPlanService.getBaseBomChild(id));
	}

}